﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clueless
{
    public class PlayerList
    {
        private LinkedList<Player> players;
        private int currentPlayer;

        public PlayerList()
        {
            players = new LinkedList<Player>();
            currentPlayer = 0;
        }
    
        public LinkedList<Player> Players
        {
            get
            {
                return players;
            }
            set
            {
                players = value;
            }
        }

        public void AddPlayer(Player p)
        {
            players.AddLast(p);
        }

        public Player GetCurrentPlayer()
        {
            return players.ElementAt(currentPlayer);
        }

        public Player GetNextPlayer()
        {
            if (currentPlayer == players.Count)
            {
                return players.ElementAt(0);
            }
            else
            {
                return players.ElementAt(currentPlayer+1);
            }
        }

        /// <summary>
        /// Determine if player exists
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public bool PlayerExists(String s)
        {
            foreach (Player p in Players)
            {
                if (p.Character.Name == s)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Find and return the request player
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public Player GetPlayer(String s)
        {
            foreach (Player p in Players)
            {
                if (p.Character.Name == s)
                    return p;
            }
            throw new Exception("Failed to locate player " + s);
        }


        public void SetCurrentPlayer(string p)
        {
            int counter = 0;
            foreach (Player player in players)
            {
                if (p == player.Character.Name)
                {
                    currentPlayer = counter;
                    break;
                }
                else
                {
                    counter++;
                }

            }
        }
    }
}
